package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.InStockConverter;
import com.goods.business.mapper.InStockMapper;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.InStock;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class InStockServiceImpl implements InStockService {
    @Autowired
    private InStockMapper inStockMapper;

    @Autowired
    private InStockConverter inStockConverter ;
    //加载表格数据
    @Override
    public PageVO findInStockList(Integer pageNum, Integer pageSize, InStockVO inStockVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(InStock.class);
        Example.Criteria criteria = example.createCriteria();
        if (inStockVO.getStatus()!=null){
            criteria.andEqualTo("status",inStockVO.getStatus());
        }

        List<InStock> inStocks = inStockMapper.selectByExample(example);

//        List<InStockVO> inStockVOS = new ArrayList<>();
//        if (inStocks!=null){
//            for (InStock inStock : inStocks) {
//                InStockVO inStockVO1 = new InStockVO();
//                BeanUtils.copyProperties(inStock, inStockVO1);
//                inStockVOS.add(inStockVO1);
//            }
//        }
        //代码同上注释
        List<InStockVO> inStockVOS1 = inStockConverter.converterToVOList(inStocks);

        PageInfo<InStock> pageInfo = new PageInfo<InStock>(inStocks);
        long total = pageInfo.getTotal();
        PageVO<InStockVO> pageVO = new PageVO<InStockVO>(total,inStockVOS1);//要total ， List数据
        return pageVO;
    }
}
